package com.hqq.concurrency.mutex;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Thief
 * Created by heqianqian on 2017/7/29.
 */
public class Thief extends Thread {

    private static Logger LOGGER = LoggerFactory.getLogger(Thief.class);

    private String name;

    private Jar jar;

    public Thief(String name, Jar jar) {
        this.name = name;
        this.jar = jar;
    }

    /**
     * 一直获取bean对象直到bean个数为0
     */
    @Override
    public void run() {
        int beans = 0;
        while (jar.takeBean()) {
            beans += 1;
            LOGGER.info("{} take a bean", name);
        }
        LOGGER.info("{} take {} beans ", name, beans);
    }
}
